package com.bienvenue.main.ui.activity.enterprise;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;

import com.bienvenue.main.R;
import com.bienvenue.main.base.BaseActivity;
import com.bienvenue.main.httpService.ResponseObserver;
import com.bienvenue.main.model.EnterprisePwdLoginModel;
import com.bienvenue.main.model.HttpBean;
import com.bienvenue.main.model.PwdLoginModel;
import com.bienvenue.main.serviceApi.ServiceModel;
import com.bienvenue.main.ui.activity.personal.MainActivity;
import com.bienvenue.main.ui.activity.personal.PersonalRegisterActivity;
import com.bienvenue.main.ui.activity.personal.PhoneSmsLoginActivity;
import com.bienvenue.main.utils.SPUtils;

import org.zackratos.ultimatebar.UltimateBar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: Bienvenue
 * @Package: com.bienvenue.main.ui.activity
 * @ClassName: PwdLoginActivity
 * @Description: 密码登录
 * @Author: Mr.Chen
 * @CreateDate: 2020/9/9 13:46
 * @UpdateUser: Mr.Chen
 * @UpdateDate: 2020/9/9 13:46
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */


public class EnterprisePwdLoginActivity extends BaseActivity {
    @BindView(R.id.et_login_phone)
    EditText etLoginPhone;

    @BindView(R.id.et_login_pwd)
    EditText etLoginPwd;

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        UltimateBar ultimateBar = new UltimateBar(this);
        ultimateBar.setColorBar(ContextCompat.getColor(this, R.color.color_38A7D4));

        ServiceModel.getServiceModel().onEnterprisePwdLogin("chen1234567", "123456", new ResponseObserver<EnterprisePwdLoginModel>() {
            @Override
            public void onSuccess(EnterprisePwdLoginModel response) {
                if (response.getCode() == 200) {
                    SPUtils.putString("token",response.getData().getToken());
                    SPUtils.putString("companyId",response.getData().getId());

                    startActivity(new Intent(EnterprisePwdLoginActivity.this, EnterpriseMainActivity.class));
                } else {
                    showToast(response.getMsg());
                }
            }

            @Override
            public void onFail(String message) {
                showToast(message);

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pwd_login;
    }

    @OnClick(R.id.iv_return_btn)
    void onFinish() {
        this.finish();
    }

    @OnClick(R.id.tv_register_btn)
    void onRegister() {
        startActivity(new Intent(EnterprisePwdLoginActivity.this, PersonalRegisterActivity.class));
        this.finish();
    }

    @OnClick(R.id.tv_sms_login_btn)
    void onSmsLogin() {
        startActivity(new Intent(EnterprisePwdLoginActivity.this, PhoneSmsLoginActivity.class));
        this.finish();
    }

    @OnClick(R.id.tv_login_btn)
    void onLogin() {
        if (TextUtils.isEmpty(etLoginPhone.getText().toString().trim())) {
            showToast("请输入正确的用户名");
        } else if (TextUtils.isEmpty(etLoginPwd.getText().toString().trim()) || etLoginPwd.getText().toString().trim().length() <= 5) {
            showToast("请输入正确的密码");
        } else {
            ServiceModel.getServiceModel().onEnterprisePwdLogin(etLoginPhone.getText().toString().trim(), etLoginPwd.getText().toString().trim(), new ResponseObserver<EnterprisePwdLoginModel>() {
                @Override
                public void onSuccess(EnterprisePwdLoginModel response) {
                    if (response.getCode() == 200) {
                        SPUtils.putString("token",response.getData().getToken());
                        SPUtils.putString("companyId",response.getData().getId());
                        startActivity(new Intent(EnterprisePwdLoginActivity.this, EnterpriseMainActivity.class));
                    } else {
                        showToast(response.getMsg());
                    }
                }

                @Override
                public void onFail(String message) {
                    showToast(message);

                }
            });
        }
    }
}
